package com.qf.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

@RestController
public class DownloadController {
///{pic}        @PathVariable String pic
    @GetMapping("/download")
    public void download(String pic, HttpServletResponse response) throws IOException {
        //获取图片路径
        String picPath="E:\\千峰课程总纲\\"+pic;
        //获取图片的字节流InoutStream
        FileInputStream inputStream=new FileInputStream(picPath);
        //获取响应的OutputStream
        ServletOutputStream outputStream = response.getOutputStream();
        //设置乡音头以符检的形式打开
        response.setHeader("Content-Disposition", "attachment;filename="+pic);
        //通过IOutils写回
        IOUtils.copy(inputStream,outputStream);
    }


}
